package com.songshuai.webapi.permission.controller;

import com.songshuai.webapi.baseapi.enumerations.ResultStatus;
import com.songshuai.webapi.baseapi.pojo.BaseParmDto;
import com.songshuai.webapi.baseapi.pojo.QueryResponse;
import com.songshuai.webapi.baseapi.utils.RequestParamValidUtil;
import com.songshuai.webapi.permission.dto.request.*;
import com.songshuai.webapi.permission.dto.response.RoleListResDto;
import com.songshuai.webapi.permission.dto.response.RoleUserListResDto;
import com.songshuai.webapi.permission.service.RolesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@Api("角色管理")
@Slf4j
@RestController
@RequestMapping("/api/roles/")
public class RolesController{

   @Autowired
    private RolesService rolesService;

    @ApiOperation(value = "查询角色分页列表")
    @PostMapping("queryRolesPageList")
    public QueryResponse<List<RoleListResDto>> queryRolesPageList(@RequestBody BaseParmDto<RolesReqDto> parmDto){
        try{
            return rolesService.queryRolesList(parmDto);
        } catch (Exception ex) {
            log.error("api/roles/queryRolesPageList", ex);
           // return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED);
            return new QueryResponse(ResultStatus.HTTP_STATUS_INTERNAL_SERVER_ERROR);
        }
    }

    @ApiOperation(value = "新增角色")
    @PostMapping("addRole")
    public QueryResponse addRole(@RequestBody @Valid  RoleAddDto roleAddDto, BindingResult result){
        if (result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        try {
            return rolesService.insert(roleAddDto);
        } catch (Exception e) {
            log.error("api/roles/addRole",e);
            return new QueryResponse(ResultStatus.HTTP_STATUS_INTERNAL_SERVER_ERROR);
        }
      // return new QueryResponse(ResultStatus.ERROR_CREATE_FAILED);
    }

    @ApiOperation(value = "修改角色信息（根据角色id）")
    @PostMapping("updateRoleById")
    public QueryResponse updateRoleById(@RequestBody @Valid RoleEditDto parmDto, BindingResult result){
        if (result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        try {
            return rolesService.updateByRoleId(parmDto);
        } catch (Exception e) {
            log.error("api/roles/updateByRoleId",e);
            return new QueryResponse(ResultStatus.HTTP_STATUS_INTERNAL_SERVER_ERROR);
        }
        //return new QueryResponse(ResultStatus.ERROR_UPDATE_FAILED);
    }

    @ApiOperation(value = "删除角色")
    @PostMapping("deleteRoleById")
    public QueryResponse deleteRoleById(@RequestBody @Valid  RoleDelReqDto roleDelReqDto, BindingResult result){
        if (result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        try {
            return rolesService.deleteRoleById(roleDelReqDto.getRoleId());
        } catch (Exception e) {
            log.error("api/roles/deleteRoleById",e);
            //return new QueryResponse(ResultStatus.FAIL.getErrorCode(), e.getMessage());
            return new QueryResponse(ResultStatus.HTTP_STATUS_INTERNAL_SERVER_ERROR);
        }
       // return new QueryResponse(ResultStatus.ERROR_DELETE_FAILED);
    }

    @ApiOperation(value = "启用禁用角色（根据角色id）")
    @PostMapping("enabledRoleById")
    public QueryResponse enabledRoleById(@RequestBody @Valid RoleEnabledDto parmDto, BindingResult result){
        if (result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        try {
            return rolesService.enabledRoleById(parmDto);
        } catch (Exception e) {
            log.error("api/roles/enabledRoleById",e);
            return new QueryResponse(ResultStatus.HTTP_STATUS_INTERNAL_SERVER_ERROR);
        }
        //return new QueryResponse(ResultStatus.ERROR_UPDATE_FAILED);
    }

    @ApiOperation(value = "查询该角色关联的用户列表")
    @PostMapping("queryUserList")
    public QueryResponse<RoleUserListResDto> queryUserList(@RequestBody @Valid  RoleUserListReqDto roleUserListReqDto, BindingResult result){
        long startTime = System.currentTimeMillis();    //获取开始时间
        if (result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        try {

            QueryResponse<RoleUserListResDto>  queryResponse=rolesService.queryUserList(roleUserListReqDto);
            long endTime = System.currentTimeMillis();    //获取结束时间
            log.info("-------Controller-------程序运行时间：" + (endTime - startTime) + "ms");    //输出程序运行时间
            return queryResponse;
        } catch (Exception e) {
            log.error("api/roles/queryUserList",e);
            //return new QueryResponse(ResultStatus.FAIL.getErrorCode(), e.getMessage());
            return new QueryResponse(ResultStatus.HTTP_STATUS_INTERNAL_SERVER_ERROR);
        }
        //return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED);
    }

    @ApiOperation(value = "删除角色用户的关联关系")
    @PostMapping("delUserRoleById")
    public QueryResponse delUserRoleById(@RequestBody @Valid  RoleUserDelReqDto roleUserDelReqDto, BindingResult result){
        if (result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        try {
             return rolesService.delUserRoleById(roleUserDelReqDto);
        } catch (Exception e) {
            log.error("api/roles/delUserRoleById",e);
            return new QueryResponse(ResultStatus.HTTP_STATUS_INTERNAL_SERVER_ERROR);
        }
        //return new QueryResponse(ResultStatus.ERROR_DELETE_FAILED);
    }

    @ApiOperation(value = "查询角色的权限列表并标识打勾项")
    @GetMapping("queryRoleNavAndFuncList")
    public QueryResponse<Map<String,Object>> queryRoleNavAndFuncList(@RequestParam("roleId")Integer roleId,@RequestParam("appId")Integer appId){
        try {
            return rolesService.queryRoleNavAndFuncList(roleId,appId);
        } catch (Exception e) {
            log.error("api/roles/queryRoleNavAndFuncList",e);
            return new QueryResponse(ResultStatus.HTTP_STATUS_INTERNAL_SERVER_ERROR);
        }
        //return new QueryResponse(ResultStatus.ERROR_SEARCH_FAILED);
    }

    @ApiOperation(value = "分配权限")
    @PostMapping("addRoleNavigationRelation")
    public QueryResponse addRoleNavigationRelation(@RequestBody  @Valid  RoleRelateNavigationReqDto parmDto, BindingResult result){
        if (result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        try {
            return rolesService.addRoleNavigationRelation(parmDto);
        } catch (Exception e) {
            log.error("api/roles/addRoleNavigationRelation",e);
            return new QueryResponse(ResultStatus.HTTP_STATUS_INTERNAL_SERVER_ERROR);
        }
       // return new QueryResponse(ResultStatus.ERROR_CREATE_FAILED);
    }

    @ApiOperation(value = "查询角色和appId对应的主页信息")
    @PostMapping("queryRoleHomePageList")
    public QueryResponse queryRoleHomePageList(@RequestBody @Valid  RoleHomePageReqDto reqDto, BindingResult result){
        if (result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        try{
            Map<String,Object> resultData= rolesService.queryHomePageInfo(reqDto);
            return QueryResponse.setResData(resultData);
        } catch (Exception e) {
            log.error("api/roles/queryRoleHomePageList",e);
            //return new QueryResponse(ResultStatus.FAIL.getErrorCode(), e.getMessage());
            return new QueryResponse(ResultStatus.HTTP_STATUS_INTERNAL_SERVER_ERROR);
        }
    }

    @ApiOperation(value = "设置角色的主页信息")
    @PostMapping("insertHomepageConfig")
    public QueryResponse insertHomepageConfig(@RequestBody @Valid  YxHomepageConfigReqDto homepageConfigReqDto, BindingResult result){
        if (result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        try {
            return rolesService.insertYxHomepageConfig(homepageConfigReqDto);
        } catch (Exception e) {
            log.error("api/roles/insertYxHomepageConfig",e);
            return new QueryResponse(ResultStatus.HTTP_STATUS_INTERNAL_SERVER_ERROR);
        }
       // return new QueryResponse(ResultStatus.ERROR_CREATE_FAILED);
    }

    @ApiOperation(value = "给角色分配人员")
    @PostMapping("insertRoleUsers")
    public QueryResponse insertRoleUsers(@RequestBody @Valid  RoleUserReqDto roleUserReqDto, BindingResult result){
        long startTime = System.currentTimeMillis();    //获取开始时间

        if (result.hasErrors()){
            return new QueryResponse(ResultStatus.ERROR_INVALID_ARGUMENT.getErrorCode(), RequestParamValidUtil.getBindingResultMessage(result));
        }
        try {
            QueryResponse queryResponse=rolesService.insertRoleUsers(roleUserReqDto);

            long endTime = System.currentTimeMillis();    //获取结束时间
            log.info("-------Controller-------程序运行时间：" + (endTime - startTime) + "ms");    //输出程序运行时间

            return queryResponse;
        } catch (Exception e) {
            log.error("api/roles/insertRoleUsers",e);
            return new QueryResponse(ResultStatus.HTTP_STATUS_INTERNAL_SERVER_ERROR);
           // return new QueryResponse(ResultStatus.ERROR_CREATE_FAILED);
        }
    }

    //该方法已经废弃未使用
   /* @ApiOperation(value = "根据appId和角色Id获取菜单列表")
    @PostMapping("queryNavigationList")
    public QueryResponse<Map<String,Object>> queryNavigationList(@RequestBody NavigationsListQueryDto parmDto){
        try{
            Map<String,Object> result= rolesService.queryNavigationList(parmDto);
            return QueryResponse.setResData(result,ResultStatus.SUCCESS);
        } catch (Exception e) {
            log.error("api/roles/queryNavigationList",e);
            return new QueryResponse(ResultStatus.FAIL.getErrorCode(), e.getMessage());
        }
    }*/

    //该方法已经废弃
    /*@ApiOperation(value = "保存角色的权限分配")
    @PostMapping("insertRoleNav igations")
    public QueryResponse insertRoleNavigations(@RequestBody RoleNavigationsAddDto roleNavigationsAddDto){
        try {
            return rolesService.insertRoleNavigations(roleNavigationsAddDto);
        } catch (Exception e){
            log.error("insertRoleNavigations",e);
        }
        return new QueryResponse(ResultStatus.ERROR_CREATE_FAILED);
    }*/

       //该方法已经废弃
   /* @ApiOperation(value = "给角色分配人员")
    @PostMapping("insertUserRoles")
    public QueryResponse insertUserRoles(@RequestParam("roleId") Integer roleId,@RequestParam("userIds") String userIds){
        boolean result=rolesService.insertUserRoles(roleId,userIds);
        if(result) {
            return new QueryResponse();
        }
        return new QueryResponse(ResultStatus.ERROR_CREATE_FAILED);
    }*/


}
